// pages/index/appraise-clazz-detail/clazz.js

const app = getApp()
const requestUtil = require("../../../utils/request");
const api = require("../../../config/api");
const util = require("../../../utils/util");
const common = require("../../../utils/common");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    appraisalTabArray: [],
    AppraisalTab: 0,
    gradeList: [], // 年级列表
    gradeListIndex: 0, // 年级列表索引
    clazzList: [], // 班级列表
    clazzListIndex: 0, // 班级列表索引
    weekList: [],
    weekListIndex: 0,
    userInfo: null,
    isOnload: false,
    tableHeader: [{
      prop: 'grade',
      width: 130,
      label: '年级'
    }, {
      prop: 'clazz',
      width: 130,
      label: '班级'
    }, {
      prop: 'plateType',
      width: 150,
      label: '版块类型'
    }, {
      prop: 'score',
      width: 150,
      label: '打分'
    }, {
      prop: 'regTeacherName',
      width: 150,
      label: '打分老师'
    }, {
      prop: 'auditTercherName',
      width: 150,
      label: '审批老师'
    }, {
      prop: 'reason',
      width: 500,
      label: '打分理由'
    },
    {
      prop: 'detail',
      width: 150,
      label: '详情'
    }], // 表头
    tableList: [], // 表格数据
    border: true,
    tableNoDataMsg: '暂无数据',
    pageNum: 1, // 页面
    pageSize: 10, // 每页数
    listTotal: 0, // 页面总数
    plateType: null, // 如果是板块负责人才会有的
    evaType: 0, // 负责人类型
    weekNo: 1, // 第几周
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let clazz = options.clazz;
    let gradeName = options.gradeName;
    let plateType = options.plateType;
    let evaType = options.evaType == 0 ? 1 : options.evaType;
    let userInfo = app.globalData.teacherInfo[0];
    let weekNo = options.weekNo;
    let isLeader = util.isNullOrEmpty(options.isLeader) ? false : true;
    let tabType = options.tabType;
    let dutyType = options.dutyType;
    this.setData({
      userInfo: userInfo,
      plateType: plateType,
      evaType: evaType,
      weekNo: weekNo,
      isLeader: isLeader,
      tabType: tabType,
      dutyType: dutyType
    });
    if(options.gradeName2) {
      this.setGradeClazz(options.gradeName2, options.clazz2);
      return;
    }
    this.getPlate(gradeName, clazz);
  },
  onShow() {
    if(this.data.isOnload) this.getItemList();
  },
  async getPlate(gradeName, clazz) {
    let that = this;
    let plateType = that.data.plateType;
    let appraisalTabArray = ['全部'];
    let AppraisalTab = 0;
    let res = await requestUtil.request("GET", api.getDictData + 'eva_class', null);
    for(let i in res.data.data) {
      appraisalTabArray.push(res.data.data[i].dictValue);
      if (!util.isNullOrEmpty(plateType)) {
        if (res.data.data[i].dictValue == plateType) {
          AppraisalTab = parseInt(i) + 1;
          break;
        }
      }
    }
    that.setData({
      appraisalTabArray: appraisalTabArray,
      AppraisalTab: AppraisalTab,
      gradeName: gradeName,
      clazz: clazz,
      isOnload: true
    })
    this.getStudentOrganize(gradeName, clazz, true);
  },
  async setGradeClazz(gradeName, clazz){
    this.setData({
      gradeList: [{"gradeName": gradeName}],
      gradeListIndex: 0,
    })
    if(clazz) {
      this.setData({
        clazzList: [{"clazz": clazz + "班"}],
        clazzListIndex: 0
      })
    }else {
      let res = await requestUtil.request('GET', api.studentOrganize, {
        schoolId: this.data.userInfo.schoolId
      })
      let list = res.data.data;
      for(let i in list) {
        if(gradeName == list[i].gradeName) {
          let clazzList = list[i].subList;
          if (clazzList && clazzList.length > 0) {
            for (let j of clazzList) {
              j.clazz += '班';
            }
          }
          let obj = {
            clazz: '全部'
          }
          clazzList.unshift(obj);
          this.setData({
            clazzList: clazzList,
            clazzListIndex: 0
          })
          break;
        }
      }
    }
    this.getWeekInfo();
  },
  /**
   * 用户上拉触底的事件
   */
  onReachBottom: function () {
    wx.showLoading({
      title: '加载中......',
      mask: true
    });
    let maxNum = Math.ceil(this.data.listTotal / this.data.pageSize);
    let num = this.data.pageNum + 1; // 页码+1
    if (num <= maxNum) {
      this.setData({
        pageNum: num
      });
      this.getItemList(true);
    }
    wx.hideLoading();
  },

  /**
   * 板块类型选择
   */
  appraisalPickerChange(e) {
    this.setData({
      AppraisalTab: e.detail.value
    })
    this.resetPage();
    this.getItemList();
  },

  /**
   * 年级列表选择
   */
  gradeListPickerChange(e) {
    this.setData({
      gradeListIndex: e.detail.value,
      clazzListIndex: 0, // 选择年级时重置班级选择
    })
    this.resetPage();
    this.getStudentOrganize(this.data.gradeList[e.detail.value].gradeName == '全部' ? null : this.data.gradeList[e.detail.value].gradeName);
    this.getItemList();
  },

  /**
   * 班级列表选择
   */
  clazzListPickerChange(e) {
    this.setData({
      clazzListIndex: e.detail.value
    })
    this.resetPage();
    this.getItemList();
  },

  /**
   * 获取年级列表
   */
  async getStudentOrganize(gradeName, clazz, flag = false) {
    let res = await requestUtil.request('GET', api.studentOrganize, {
      schoolId: this.data.userInfo.schoolId
    })
    let data = res.data.data;
    let clazzList = [];
    let gradeIndex = 0;
    let clazzIndex = 0;
    let obj2 = {
      gradeName: '全部',
      studyType: 1
    }
    data.unshift(obj2);
    // 默认有选择年级才这个
    if (!util.isNullOrEmpty(gradeName)) {
      for (let i in data) {
        if (data[i].gradeName == gradeName) {
          clazzList = data[i].subList;
          gradeIndex = i;
          break;
        }
      }
    } else {
      // clazzList = data[0].subList;
      clazzList = [];
    }
    if (!util.isNullOrEmpty(clazz)) {
      for (let i in clazzList) {
        if (clazzList[i].clazz == clazz) {
          clazzIndex = ++i; // 因为后面给ClazzList添加了一项
          break;
        }
      }
    }
    if (clazzList && clazzList.length > 0) {
      for (let j of clazzList) {
        j.clazz += '班';
      }
    }
    let obj = {
      clazz: '全部'
    }
    clazzList.unshift(obj);
    // 清空年级列的班级数组，防止数据过大造成setData卡顿
    for (let i of data) {
      i.subList = null;
    }
    this.setData({
      gradeList: data,
      clazzList: clazzList,
      gradeListIndex: gradeIndex,
      clazzListIndex: clazzIndex
    });
    if (flag) {
      this.getWeekInfo();
    }
  },

  /**
   * 获取周列表
   */
  async getWeekInfo() {
    //FIXME 其实这里用currDutyTeacher接口会简单点，但我懒得改了
    let res = await requestUtil.request("GET", api.dutyWeekList, {
      schoolId: this.data.userInfo.schoolId
    })
    let data = res.data.data;
    let weekList = [];
    let weekListIndex = 0;
    for (let i in data) {
      let startDate = common.getStringMonthAndDay(data[i].weekStartDate);
      let endDate = common.getStringMonthAndDay(data[i].weekEndDate);
      let str = '第' + data[i].weekNo + '周，' + startDate + ' - ' + endDate;
      let obj = {
        termId: data[i].termId,
        termName: data[i].termName,
        weekNo: data[i].weekNo,
        date: str
      }
      weekList.push(obj);
      if (data[i].weekNo == this.data.weekNo) {
        weekListIndex = parseInt(i);
      }
    }
    this.setData({
      weekList,
      weekListIndex
    });
    this.getItemList();
  },

  /**
   * 星期列表选择
   */
  weekListPickerChange(e) {
    this.setData({
      weekListIndex: e.detail.value,
      weekNo: this.data.weekList[e.detail.value].weekNo
    })
    this.getItemList();
  },

  /**
   * 重置分页
   */
  resetPage() {
    this.setData({
      pageNum: 1,
      pageSize: 10
    })
  },

  /**
   * 获取板块负责人的表格数据
   */
  async getItemList(page = false) {
    let params = {
      schoolId: this.data.userInfo.schoolId,
      termId: this.data.weekList[this.data.weekListIndex].termId,
      weekNo: this.data.weekNo,
      gradeName: this.data.gradeList[this.data.gradeListIndex].gradeName,
      clazz: this.data.clazzList[this.data.clazzListIndex].clazz,
      plateType: this.data.evaType == 1 ? this.data.appraisalTabArray[this.data.AppraisalTab] : this.data.plateType,
      fyNum: this.data.pageNum,
      fySize: this.data.pageSize,
      status: "1"
    }
    if (params.gradeName == '全部') {
      params.gradeName = null;
    }
    if (params.plateType == '全部') {
      params.plateType = null;
    }
    if (params.clazz == '全部') {
      params.clazz = null;
    } else {
      let clazz = params.clazz.substr(0, params.clazz.length - 1);
      params.clazz = Number(clazz);
    }
    let res = await requestUtil.request("POST", api.getItemList, params);
    let data = res.data.data.rows;

    let list = [];
    if (page == true) {
      list = this.data.tableList;
    }
    for (let i of data) {
      let obj = {
        id: i.id,
        grade: i.gradeName,
        plateType: i.plateType,
        clazz: i.clazz + '班',
        score: i.score,
        regTeacherName: i.regTeacherName,
        auditTercherName: i.auditTercherName,
        reason: i.reason,
        detail: '详情'
      }
      list.push(obj);
    }
    this.setData({
      tableList: list,
      listTotal: res.data.data.total
    })
  },
  /**
   * 文明评比详情
   */
  onRowClick(e) {
    let rowData = e.detail.currentTarget.dataset.it;
    let tabType = this.data.tabType;
    let dutyType = this.data.dutyType;
    wx.navigateTo({
      url: '../appraise-detail/detail?id=' + rowData.id +
      '&tabType=' + tabType +
      '&dutyType=' + dutyType,
    })
  },
})